package com.md.youyouhui.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityMainBinding;
import com.md.youyouhui.ui.fragment.HomeFragment;
import com.md.youyouhui.ui.fragment.UserFragment;
import com.md.youyouhui.ui.home.InformationFragment;
import com.md.youyouhui.ui.shopping.ShoppingFragment;
import com.md.youyouhui.utils.AddFragmentShow;
import com.md.youyouhui.utils.StatusBarHelper;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private ActivityMainBinding mainBinding;
    private AddFragmentShow fragmentShow;

    @Override
    public void setContentLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StatusBarHelper.setFullWindow(getWindow());
            StatusBarHelper.setStatusBarTextColor(getWindow(), true);
        }
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    }


    @Override
    public void initView() {
       // mainBinding.mainPay.setOnClickListener(v -> startActivity(new Intent(ct, PayOilActivity.class)));
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                }, 1);
            }

        }
        setTabState( mainBinding.home, R.drawable.menu_home_bg_press, Color.parseColor("#E54B4B"));//设置Tab状态
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                String permission = permissions[i];
                int grantResult = grantResults[i];
                if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    } else {
                        Toast.makeText(this, "权限没有打开,会导致程序出错", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }

    @Override
    public void initData() {
        fragmentShow = new AddFragmentShow();
        fragmentShow.addFragmentShow(this, R.id.ma_frame_layout, HomeFragment.class);
        mainBinding.radioGroup.setOnCheckedChangeListener(this);

    }

    public void resetTabState() {
        setTabState( mainBinding.home, R.drawable.menu_home_bg, Color.parseColor("#E2E2E2") );
        setTabState( mainBinding.mainPay, R.drawable.menu_save_bg, Color.parseColor("#E2E2E2") );
        setTabState( mainBinding.mainNews, R.drawable.menu_news_bg, Color.parseColor("#E2E2E2") );
        setTabState(  mainBinding.user, R.drawable.menu_user_bg, Color.parseColor("#E2E2E2"));
    }






    private void setTabState(RadioButton radioButton, int image, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
           // radioButton.setButtonDrawable(image);
            radioButton.setCompoundDrawablesRelativeWithIntrinsicBounds( 0, image, 0, 0 );//Call requires API level 17
        }
        radioButton.setTextColor( color );
    }




    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        resetTabState();
        switch (checkedId) {
            case R.id.home:
                setTabState( mainBinding.home, R.drawable.menu_home_bg_press, Color.parseColor("#E54B4B"));//设置Tab状态
                fragmentShow.addFragmentShow(this, R.id.ma_frame_layout, HomeFragment.class);
                break;
            case R.id.mainPay:
                setTabState( mainBinding.mainPay, R.drawable.menu_save_bg_press, Color.parseColor("#E54B4B"));//设置Tab状态
                fragmentShow.addFragmentShow(this, R.id.ma_frame_layout, ShoppingFragment.class);
                break;
            case R.id.mainNews:
                setTabState( mainBinding.mainNews, R.drawable.menu_news_bg_press, Color.parseColor("#E54B4B"));//设置Tab状态
                fragmentShow.addFragmentShow(this, R.id.ma_frame_layout, InformationFragment.class);
                break;
            case R.id.user:
                setTabState( mainBinding.user, R.drawable.menu_user_bg_press, Color.parseColor("#E54B4B"));//设置Tab状态
                fragmentShow.addFragmentShow(this, R.id.ma_frame_layout, UserFragment.class);
                break;
            default:
                break;
        }
    }
}
